#include "mygraphicsscene.h"

MyGraphicsScene::MyGraphicsScene(QObject *parent) :
    QGraphicsScene(parent)
{
    _scale = 1;
    _scaleStep = 0.5;
    _aktualTime = 0;
    QPen pen(Qt::green);
    _line = this->addLine(0, 0, 0, 0, pen);
}


//dopsat vykreslovani os a popisku os

void MyGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    if(-painter->transform().dx() <_scaledAktualTime && (-painter->transform().dx())+rect.width() > _scaledAktualTime)
    {
        painter->setPen(QColor(Qt::green));
        painter->drawLine(_scaledAktualTime, 0, _scaledAktualTime, 200);
    }
}

void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->text() == QString("myItem"))
    {
        event->setAccepted(true);
    }
}

void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->text() == QString("myItem"))
    {
        if(event->mimeData()->hasFormat(QString("pointer")))
        {
            MyGraphicsItem *item = (MyGraphicsItem*)(event->mimeData()->data(QString("pointer")).toLongLong());
            qreal x = event->scenePos().x( )- event->mimeData()->data(QString("hotSpot")).toFloat();
            if(x + item->timeLenghtScaled() >= sceneRect().width())
                x = sceneRect().width() - item->timeLenghtScaled();
            if(x <= 0)
                x = 0;
            item->setXPos(x);
        }
    }
}

void MyGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->hasFormat(QString("new")))
    {
        MyGraphicsItem *item = new MyGraphicsItem;
        item->setMyType(QString().append(event->mimeData()->data(QString("type"))));
        item->setScaleX(_scale);
        QColor color = QColor(QString().append(event->mimeData()->data("color")));
        item->setColor(color);
        item->setToolTip(QString().append(event->mimeData()->data(QString("new"))));
        qreal x = event->scenePos().x();
        if(x + item->timeLenghtScaled() >= sceneRect().width())
            x = sceneRect().width() - item->timeLenghtScaled();
        if(x <= 0)
            x = 0;
        item->setPos(x, LINE_HEIGHT * _typesList.indexOf(QString().append(event->mimeData()->data(QString("type")))));
        addItem(item);
    }
}

void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem *item = QGraphicsScene::itemAt(event->scenePos());
    if(item != NULL && (void*)item != (void*)_line)
    {
        emit mousePressed((MyGraphicsItem*)item);
    }
    QGraphicsScene::mousePressEvent(event);
}

void MyGraphicsScene::setScaleStep(qreal step)
{
    _scaleStep = step;
}

qreal MyGraphicsScene::scaleStep(void)
{
    return _scaleStep;
}

void MyGraphicsScene::scaleIn(void)
{
    _scale /= _scaleStep;
    scaleUpdate();
    updateSceneRectSize();
    update();
    qDebug()<<"scale: "<<_scale;
    qDebug()<<sceneRect();
}

void MyGraphicsScene::scaleOut(void)
{
    _scale *= _scaleStep;
    scaleUpdate();
    updateSceneRectSize();
    update();
    qDebug()<<sceneRect();
}

void MyGraphicsScene::scaleUpdate(void)
{
    QList<QGraphicsItem*> list = QGraphicsScene::items();
    MyGraphicsItem *item;
    for(int i = 0; i < list.size(); i++)
    {
        item = (MyGraphicsItem*)list.at(i);
        if((void*)item == (void*)_line)
        {
            break;
        }
        item->setScaleX(_scale);
    }
}

void MyGraphicsScene::setTotalTime(quint64 time)
{
    _totalTime = time;
    updateSceneRectSize();
}

qint64 MyGraphicsScene::totalTime(void) const
{
    return _totalTime;
}

void MyGraphicsScene::addType(QString str)
{
    _typesList.append(str);
}

QStringList MyGraphicsScene::typeList() const
{
    return _typesList;
}

void MyGraphicsScene::updateSceneRectSize()
{
    _scaledTotalTime = _totalTime * _scale;
    setSceneRect(QRectF(0, 0, _scaledTotalTime, _typesList.size() * LINE_HEIGHT));
}

void MyGraphicsScene::tick(qint64 time)
{
    _aktualTime = time;
    _scaledAktualTime = time * _scale;
    update();
}
